package com.aiti.lulian.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.lulian.entity.LuLianEnterprise;
import com.aiti.lulian.gxmh.dto.GxmhPolicyVisitParamDto;
import com.aiti.lulian.gxmh.dto.GxmhPolicyVisitVo;
import com.aiti.lulian.gxmh.dto.SysUserDto;
import com.aiti.lulian.gxmh.entity.GxmhPolicyVisit;
import com.aiti.lulian.mapper.LuLianEnterpriseMapper;
import com.aiti.lulian.mapper.gxmh.GxmhPolicyVisitMapper;
import com.aiti.lulian.mapper.gxmh.GxmhSysMapper;
import com.aiti.lulian.service.GxmhPolicyVisitService;
import com.aiti.lulian.utils.BusinessUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-07-29 18:03
 **/
@Service
@Slf4j
public class GxmhPolicyVisitServiceImpl extends BaseSerivceImpl<GxmhPolicyVisitMapper, GxmhPolicyVisit> implements GxmhPolicyVisitService {

    @Autowired
    private GxmhSysMapper gxmhSysMapper;
    @Autowired
    private LuLianEnterpriseMapper luLianEnterpriseMapper;

    /**
     * 记录政策的访问人，对于每一个政策，仅记录第一个访问该政策的用户信息和企业信息。
     * 后续相同企业的人员仅记录访问次数
     * @param gxmhPolicyVisit
     */
    public void insertPolicyVisit(GxmhPolicyVisit gxmhPolicyVisit) {
        if (StrUtil.isBlank(gxmhPolicyVisit.getUserId()) || StrUtil.isBlank(gxmhPolicyVisit.getPolicyId()) || StrUtil.isBlank(gxmhPolicyVisit.getEnterpriseId())) {
            throw new BusinessException("参数错误");
        }
        LambdaQueryWrapper<GxmhPolicyVisit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GxmhPolicyVisit::getPolicyId, gxmhPolicyVisit.getPolicyId())
                .eq(GxmhPolicyVisit::getEnterpriseId, gxmhPolicyVisit.getEnterpriseId())
                .eq(GxmhPolicyVisit::getIsDelete, 0);
        GxmhPolicyVisit existVisit = this.baseMapper.selectOne(wrapper);
        if(existVisit != null) {
            this.baseMapper.updateClickCount(existVisit.getBaseId(), gxmhPolicyVisit.getUserId());
            return;
        }
        Date now = new Date();
        SysUserDto sysUserDto = gxmhSysMapper.queryUserInfo(gxmhPolicyVisit.getUserId());
        gxmhPolicyVisit.setBaseId(IdUtil.simpleUUID());
        gxmhPolicyVisit.setUserId(gxmhPolicyVisit.getUserId());
        gxmhPolicyVisit.setPolicyId(gxmhPolicyVisit.getPolicyId());
        gxmhPolicyVisit.setEnterpriseId(gxmhPolicyVisit.getEnterpriseId());
        gxmhPolicyVisit.setClickCount(1);
        gxmhPolicyVisit.setBaseCreateTime(now);
        gxmhPolicyVisit.setBaseUpdateTime(now);
        gxmhPolicyVisit.setBaseCreateBy(gxmhPolicyVisit.getUserId());
        gxmhPolicyVisit.setBaseUpdateBy(gxmhPolicyVisit.getUserId());
        gxmhPolicyVisit.setIsDelete(0);
        gxmhPolicyVisit.setUserMobile(sysUserDto.getMobile());
        gxmhPolicyVisit.setUserRealname(sysUserDto.getRealname());
        this.save(gxmhPolicyVisit);
    }

    /**
     * 查询政策访问列表
     * @param queryDto
     * @return
     */
    @Override
    public PageResult<GxmhPolicyVisitVo> queryPolicyVisitList(GxmhPolicyVisitParamDto queryDto) {
        BusinessUtil.checkPageParam(queryDto);
        if(StrUtil.isBlank(queryDto.getPolicyId())) {
            throw new BusinessException("参数错误");
        }
        IPage<GxmhPolicyVisitVo> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        List<GxmhPolicyVisitVo> list = this.baseMapper.queryPolicyVisitList(page, queryDto);
        PageResult<GxmhPolicyVisitVo> pageResult = new PageResult<>(list, new PageInfo(queryDto.getPageNum(), queryDto.getPageSize()), page.getTotal());
        return pageResult;
    }
}
